package com.wjf.wjfaiagent.converter;

import com.wjf.wjfaiagent.entity.ChatMessage;
import org.springframework.ai.chat.messages.*;

import java.util.List;
import java.util.Map;

/**
 * 消息格式转换器
 */
public class MessageConverter {
    /**
     * Message => ChatMessage
     * @param conversationId
     * @param message
     * @return
     */
    public static ChatMessage toChatMessage(String conversationId, Message message){
        ChatMessage chatMessage = ChatMessage.builder()
                .conversationId(conversationId)
                .messageType(message.getMessageType())
                .content(message.getText())
                .metadata(message.getMetadata())
                .build();
        return chatMessage;
    }

    /**
     * ChatMessage => Message
     * @param chatMessage
     * @return
     */
    public static Message toMessage(ChatMessage chatMessage){
        MessageType messageType = chatMessage.getMessageType();
        String content = chatMessage.getContent();
        Map<String, Object> metadata = chatMessage.getMetadata();
        return switch (messageType){
            case USER -> new UserMessage(content);
            case SYSTEM -> new SystemMessage(content);
            case ASSISTANT -> new AssistantMessage(content, metadata);
            case TOOL -> new ToolResponseMessage(List.of(), metadata);
        };
    }


}
